<!--
 * @Author: tkp
 * @Date: 2024-06-04 11:42:26
-->
<template>
  <div class="draw_contain">
    <el-form :model="queryForm" label-position="right" :rules="rules" ref="ruleForm" label-width="90px">
      <el-form-item prop="systemId" label="所属系统:">
        <el-radio-group v-model="queryForm.systemId" @input="inputHandle">
          <el-radio v-for="(item) in sysTeamType" :key="item.systemId" :label="item.systemId">{{ item.systemName }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="roleName" label="角色名称:">
        <Input v-model="queryForm.roleName" size="medium" disabled clearable placeholder="请输入角色名称"></Input>
      </el-form-item>
      <el-form-item prop="copyRoleId" label="参照角色:">
        <Select size="medium" placeholder="请选择角色" @select="changeCopyRole" :clearable="false" v-model="queryForm.copyRoleId" :data="roleList" :jsonType="{'value':'roleId','label':'roleName'}"></Select>
      </el-form-item>
      <el-form-item prop="menuIds" label="菜单权限:">
        <el-tree class="tree-border" :data="menuList" :check-strictly="true" show-checkbox ref="menu" node-key="menuId" empty-text="暂无数据" :props="defaultProps" :default-checked-keys="queryForm.menuIds">
          <span slot-scope="{ node }">
            <span>{{node.data.menuName}}</span>
          </span>
        </el-tree>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="medium" @click="okPop">确认</el-button>
        <el-button size="medium" @click="closePop">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { authMenuTreeList, authRoleQuery, authMenuStrategyQueryList, authSystemQuery } from '@/utils/api'
export default {
  props: {
    data: {
      type: Object | String,
      required: true
    }
  },
  data() {
    return {
      queryForm: {
        menuIds: [],
        roleId: '',
        roleName: '',
        copyRoleId: '',
        systemId: '',
      },
      menuList: [],
      sysTeamType: [],
      defaultProps: {
        children: "children",
        label: "menuTitle"
      },
      roleList: [],
      rules: {
        roleName: [
          { required: true, message: '此项为必填' },
        ],
        // menuIds: [
        //   { required: true, message: '此项为必填' },
        // ],
      },
    }
  },
  mounted() {
    this.loadSystem()
    this.queryForm.roleId = this.data.roleId
    this.queryForm.roleName = this.data.roleName

    this.getRole()
    this.getRoleMenuDetail()
  },
  methods: {
    closePop() {
      this.$emit('roleMenuData', '')
    },
    okPop() {
      let checkedKeys = this.$refs.menu.getCheckedKeys()
      let halfCheckedKeys = this.$refs.menu.getHalfCheckedKeys()
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      this.queryForm.menuIds = checkedKeys
      let param = {
        roleId: this.queryForm.roleId,
        menuIds: this.queryForm.menuIds,
        systemId: this.queryForm.systemId,
      }
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.$emit('roleMenuData', param)
        } else {
          this.messageWarnPop('请填写完整')
          return false;
        }
      });
    },
    changeCopyRole(e) {
      let param = {
        roleId: e,
        systemId: this.queryForm.systemId,
      }
      authMenuStrategyQueryList(param).then(res => {
        let data = res.data || []
        this.queryForm.menuIds = []
        data.forEach(item => {
          this.queryForm.menuIds.push(item.menuId)
        })
        this.$refs.menu.setCheckedNodes(this.queryForm.menuIds)
        this.$nextTick(() => {
          this.$refs.menu.$forceUpdate();
        })
      })
    },

    getRole() {
      let param = {}
      authRoleQuery(param).then(res => {
        this.roleList = res.data || []
      })
    },
    getRoleMenuDetail() {
      let param = {
        roleId: this.data.roleId,
        systemId: this.queryForm.systemId,
      }
      authMenuStrategyQueryList(param).then(res => {
        let data = res.data || []
        this.queryForm.menuIds = []
        data.forEach(item => {
          this.queryForm.menuIds.push(item.menuId)
        })
      })
    },
    inputHandle(e) {
      this.queryForm.copyRoleId = ''
      this.getMenu()
      this.getRoleMenuDetail()
    },
    loadSystem() {
      let param = {
        enable: 1
      }
      authSystemQuery(param).then(res => {
        this.sysTeamType = res.data || []
        this.queryForm.systemId = this.sysTeamType[0].systemId
        this.getMenu()
      })
    },
    getMenu() {
      let param = {
        enable: 1,
        systemId: this.queryForm.systemId,
      }
      authMenuTreeList(param).then(res => {
        this.menuList = res.data || []
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.draw_contain {
  padding: 0 20px;
}
/deep/ .el-radio {
  margin-bottom: 10px;
}
</style>
